package com.shadowtorcher.stdisk.filter;

import com.shadowtorcher.stdisk.bean.UserBean;
import com.shadowtorcher.stdisk.util.HttpResponser;
import com.shadowtorcher.stdisk.util.StaticUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebFilter(filterName = "LoginFilter", urlPatterns = {
        "/api/listfile",
        "/api/searchfile",
        "/api/getdouwnloadurl",
        "/api/deletefile",
        "/api/logoff",
        "/api/getspace",
        "/api/checkmd5andhash",
        "/api/uploadfile",
        "/api/uploadfilebyshare",
        "/api/sharemessagekeep"
})
public class LoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletResponse httpServletResponse = (HttpServletResponse) resp;
        HttpResponser httpResponser = new HttpResponser();
        UserBean user = StaticUtils.getUser(((HttpServletRequest) req).getSession());
        if (user == null) {
            httpResponser.setCode(403);
            httpResponser.setMessage("user not login");
            httpResponser.response(httpServletResponse);
            return;
        }
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
